package c2.cross.business.rank.manager;

import c2.cross.business.rank.CrossRankDao;
import c2.cross.business.rank.CrossRankModule;
import c2.cross.business.rank.h.*;
import cate.common.table.d.GDRank;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class CrossRankPermanentManager extends BaseCrossRankFuncManager{
    final Map<Integer, BaseSingleValueRankHandler> handlers = new HashMap<>();
    GuildCrossRankHandler guildHandler;
    @Override
    public void initSelf(CrossRankModule parent) {
        final CrossRankDao db = parent.getDb();
        handlers.put(GDRank.Id.CROSS_POWER_RANK_ID, new PowerCrossRankHandler(GDRank.Id.CROSS_POWER_RANK_ID, db));
        handlers.put(GDRank.Id.HOLY_PET_SUPPORT, new HolyPetSupportRankHandler(GDRank.Id.HOLY_PET_SUPPORT, db, parent.getKv(), parent.getHolyPetTable(), parent.getCommon()));
        guildHandler = new GuildCrossRankHandler(GDRank.Id.CROSS_GUILD_RANK_ID, db);
    }

    @Override
    public void tick() {
        handlers.values().forEach(BaseSingleValueRankHandler::tick);
        guildHandler.tick();
    }

    @Override
    public boolean focusRankId(int rankId) {
        return handlers.containsKey(rankId);
    }

    @Override
    public Optional<ICrossRankHandler> tryGetCrossSingleRankHandler(int rankId) {
        return Optional.ofNullable(handlers.get(rankId));
    }

    public GuildCrossRankHandler getGuildHandler() {
        return guildHandler;
    }

    public void save() {
        handlers.values().forEach(BaseSingleValueRankHandler::save);
        guildHandler.save();
    }
}
